package team.seekers.hula.island.postComment.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.island.post.dao.IslandPostDao;
import team.seekers.hula.island.post.entity.dao.IslandPostDO;
import team.seekers.hula.island.postComment.dao.IslandPostCommentDao;
import team.seekers.hula.island.postComment.entity.dao.IslandPostCommentDO;
import team.seekers.hula.island.postComment.entity.dto.*;
import team.seekers.hula.island.postComment.entity.vo.PostCommentManagePageVO;
import team.seekers.hula.user.user.service.UserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author ekin
 * @Description GOOD
 * @mood happy
 */
@Service
public class IslandPostCommentManageService {

    @Autowired
    private IslandPostCommentDao islandPostCommentDao;

    @Resource
    private UserService userService;

    @Autowired
    private IslandPostDao islandPostDao;

    public String postComment(PostCommentDTO postCommentDTO) {
        IslandPostCommentDO islandPostCommentDO = new IslandPostCommentDO();
        BeanUtils.copyProperties(postCommentDTO,islandPostCommentDO);
        islandPostCommentDao.save(islandPostCommentDO);
        return islandPostCommentDO.getId();
    }

    public PageResult<PostCommentManagePageVO> pageComment(PostCommentQueryDTO queryDTO) {
        LambdaQueryChainWrapper<IslandPostCommentDO> lambdaQuery = islandPostCommentDao.lambdaQuery();

        if(!org.springframework.util.StringUtils.isEmpty(queryDTO.getNickname())){
            List<String> userIds =  userService.listIdsLikeNickname(queryDTO.getNickname());
            if(userIds != null && userIds.size() > 0){
                lambdaQuery.in(IslandPostCommentDO::getUserId,userIds);
            }else {
                return new PageResult<>();
            }
        }

        if(!org.springframework.util.StringUtils.isEmpty(queryDTO.getPostId())){
            lambdaQuery.eq(IslandPostCommentDO::getPostId, queryDTO.getPostId());
        }

        if(!org.springframework.util.StringUtils.isEmpty(queryDTO.getUserId())){
            lambdaQuery.eq(IslandPostCommentDO::getUserId, queryDTO.getUserId());
        }

        if(!org.springframework.util.StringUtils.isEmpty(queryDTO.getIslandId())){
            lambdaQuery.eq(IslandPostCommentDO::getIslandId, queryDTO.getIslandId());
        }

        if(!org.springframework.util.StringUtils.isEmpty(queryDTO.getContent())){
            lambdaQuery.like(IslandPostCommentDO::getContent,queryDTO.getContent());
        }

        if(!org.springframework.util.StringUtils.isEmpty(queryDTO.getId())){
            lambdaQuery.like(IslandPostCommentDO::getId,queryDTO.getId());
        }

        lambdaQuery.orderByDesc(IslandPostCommentDO::getGmtCreate);

        Page<IslandPostCommentDO> page = lambdaQuery.page(new Page<>(queryDTO.getPageNow(), queryDTO.getPageSize()));
        long total = page.getTotal();
        List<IslandPostCommentDO> records = page.getRecords();
        if(records == null){
            return null;
        }
        List<PostCommentManagePageVO> list = new ArrayList<>();
        for (IslandPostCommentDO record : records) {
            PostCommentManagePageVO postCommentPageVO = new PostCommentManagePageVO();
            BeanUtils.copyProperties(record,postCommentPageVO);
            IslandPostDO one = islandPostDao.getOne(new QueryWrapper<IslandPostDO>().eq("id", record.getPostId()));
            if(null != one){
                Optional.of(one).map(IslandPostDO::getTitle).ifPresent(postCommentPageVO::setTitle);
            }
            postCommentPageVO.setUserInfo(userService.getBasicInfoVO(record.getUserId()));
            list.add(postCommentPageVO);
        }
        return new PageResult<>(total,list);


    }

    public void updatePostComment(PostCommentUpdateDTO updateDTO) {
        IslandPostCommentDO islandPostCommentDO = islandPostCommentDao.getById(updateDTO.getId());
        BeanUtils.copyProperties(updateDTO, islandPostCommentDO);
        islandPostCommentDao.updateById(islandPostCommentDO);
    }
}
